package io.github.betacatcode.demo.controller;

import io.github.betacatcode.dao.mapper.AccountMapper;
import io.github.betacatcode.command.AccountCommand;
import io.github.betacatcode.demo.common.web.R;
import io.github.betacatcode.executor.AccountExecutor;
import io.github.betacatcode.model.Money;
import io.github.betacatcode.vo.AccountVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/account")
public class AccountController {
    private final AccountExecutor accountExecutor;

    private final AccountMapper accountMapper;

    @PostMapping("/transfer")
    public R<Void> transfer(AccountCommand.TransferCommand command) throws Exception {
        accountExecutor.transfer(
                command.getSourceAccountNumber(), command.getTargetAccountNumber(), new Money(command.getAmount()));
        return R.ok();
    }

    @GetMapping("/list")
    public R<List<AccountVO>> list() {
        return R.ok(accountMapper.selectAccounts());
    }

}
